package com.liuyk.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import android.util.AttributeSet;

/**
 * 圆形图
 * <p>
 * Created by liuyk on 2019/3/28.
 */
public class CircleImageView extends androidx.appcompat.widget.AppCompatImageView {
    private final Matrix mShaderMatrix = new Matrix();

    private Paint mShaderPaint;

    private BitmapShader mBitmapShader;

    private Bitmap mBitmap;

    private final boolean isCircle = true;

    public CircleImageView(Context context) {
        this(context, null);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Bitmap bitmap) {
        if (bitmap == null) {
            invalidate();
            return;
        }

        mBitmapShader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
        mShaderPaint = new Paint();
        mShaderPaint.setShader(mBitmapShader);

        invalidate();
//        scaleBitmap();
    }

    @Override
    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.TRANSPARENT);
        if (mShaderPaint != null) {
//            canvas.drawCircle(100, 100, mBitmap.getHeight() / 2, mShaderPaint);
            canvas.drawRoundRect(0, 0, getWidth(), getHeight(), getWidth() / 2, getWidth() / 2, mShaderPaint);
        }
    }

    @Override
    public void setImageBitmap(Bitmap bm) {
        super.setImageBitmap(bm);
        mBitmap = bm;
    }

    @Override
    public void setImageDrawable(@Nullable Drawable drawable) {
        super.setImageDrawable(drawable);
        mBitmap = getBitmap(drawable);
        if (isCircle) {
//            Bitmap b = clipBitmap(mBitmap);
            init(mBitmap);
        } else {
            init(mBitmap);
        }
    }

    private Bitmap getBitmap(Drawable drawable) {
        Bitmap bitmap;

        if (drawable == null) {
            return null;
        }

        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }

    private Bitmap clipBitmap(Bitmap mBitmap) {
        if (mBitmap == null) {
            return null;
        }
        int bitWidth = mBitmap.getWidth();
        int bitHeight = mBitmap.getHeight();
        int squareWidth;

        float x = 0;
        float y = 0;
        if (bitWidth > bitHeight) {
            squareWidth = bitHeight;
            x = (bitWidth - bitHeight) / 2f;
        } else {
            squareWidth = bitWidth;
            y = (bitHeight - bitWidth) / 2f;
        }

        Bitmap squareBitmap = Bitmap.createBitmap(mBitmap, (int) x, (int) y, squareWidth, squareWidth, mShaderMatrix, true);

        return squareBitmap;
    }

    private void scaleBitmap() {
        post(new Runnable() {
            @Override
            public void run() {
                float wscale = getWidth() / mBitmap.getWidth();
                float hscale = getHeight() / mBitmap.getHeight();
                mShaderMatrix.setScale(wscale, hscale);
                mBitmapShader.setLocalMatrix(mShaderMatrix);
            }
        });
    }
}
